package lib.quasar.widget.triangle;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.CornerPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import lib.quasar.widget.R;

/**
 * description: 三角形
 * created by kalu on 2018/3/23 11:15
 */
public class TriangleView extends View {

    private final Paint mPaint = new Paint();
    private int colorBg = Color.GRAY;
    private float lineWidth = 1f;

    private boolean isTop;
    private boolean isLeft;

    public TriangleView(Context context) {
        this(context, null, 0);
    }

    public TriangleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TriangleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray array = null;
        try {
            array = context.obtainStyledAttributes(attrs, R.styleable.TriangleView);
            colorBg = array.getColor(R.styleable.TriangleView_tv_background_color, colorBg);
            isTop = array.getBoolean(R.styleable.TriangleView_tv_location_top, false);
            isLeft = array.getBoolean(R.styleable.TriangleView_tv_location_left, false);
            lineWidth = array.getDimension(R.styleable.TriangleView_tv_line_width, lineWidth);
        } catch (Exception e) {
        } finally {
            if (null == array) return;
            array.recycle();
        }
    }

    @Override
    public void draw(Canvas canvas) {

        mPaint.setAntiAlias(true);
        mPaint.setFilterBitmap(true);
        mPaint.setDither(true);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeWidth(lineWidth);
        mPaint.setPathEffect(new CornerPathEffect(5f)); // 拐点顺滑

        if (isTop) {
            final int height = getHeight() + 5;
            final int width = getWidth();
            final Path path = new Path();
            path.moveTo(0, height);// 此点为多边形的起点
            path.lineTo(width / 2, 0);
            path.lineTo(width, height);
          //  path.close(); // 使这些点构成封闭的多边形

            mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
            mPaint.setColor(Color.WHITE);
            canvas.drawPath(path, mPaint);

            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setColor(colorBg);
            canvas.drawPath(path, mPaint);

        } else if (isLeft) {
            final int height = getHeight() + 5;
            final int width = getWidth();
            final Path path = new Path();
            path.moveTo(width, 2.5f);
            path.lineTo(width * 0.5f, height * 0.4f);
            path.lineTo(width, height*0.7f);
           // path.close();

            mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
            mPaint.setColor(Color.WHITE);
            canvas.drawPath(path, mPaint);

            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setColor(colorBg);
            canvas.drawPath(path, mPaint);
        }
        super.draw(canvas);
    }
}
